<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ExpertController extends Controller
{
    public function index()
    {
        return view("front.index");
    }
    public function create()
    {
        return view('admin.expert-create');

    }
    public function store(Request $request)
    {
        $datas = $request->all();

        $rules = [
            'name' => 'required',
            'keshi' => 'required',
            'zhicheng' => 'required',
            'zhiwu' => 'required',
            'yiyuan' => 'required',
            'jianli' => 'required',
            'jieshoujiaoyu' => 'required'
        ];
        $message = [
            'name.required' => '姓名不能为空',
            'keshi.required' => '科室名不能为空',
            'zhicheng.required' => '职称不能为空',
            'zhiwu.required' => '职务不能为空',
            'yiyuan.required' => '医院不能为空',
            'jianli.required' => '简介不能为空',
            'jieshoujiaoyu.required' => '接受教育不能为空',

        ];
        $validate=Validator::make($datas,$rules,$message);
        if($validate->fails())
        {
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }else{
            if($request->hasFile('thumb'))
            {
                $file=$request->file('thumb');
                $ext=$file->extension();
                $filename=time().'.'.$ext;
                $file->move(public_path().'/upload',$filename);

                $datas['thumb']='/upload/'.$filename;
            }

        Expert::create($datas);
        return json_encode(['success' => true, 'msg' => 'success']);}





    }
    public function edit(Expert $oExperts)
    {

        return view('admin.expert-edit') ->with('oExperts',$oExperts);
    }
    public  function update($id,Request $request)
    {


        $datas = $request->all();

        $rules = [
            'name' => 'required',
            'keshi' => 'required',
            'zhicheng' => 'required',
            'zhiwu' => 'required',
            'yiyuan' => 'required',
            'jianli' => 'required',
            'jieshoujiaoyu' => 'required'
        ];
        $message = [
            'name.required' => '姓名不能为空',
            'keshi.required' => '科室名不能为空',
            'zhicheng.required' => '职称不能为空',
            'zhiwu.required' => '职务不能为空',
            'yiyuan.required' => '医院不能为空',
            'jianli.required' => '简介不能为空',
            'jieshoujiaoyu.required' => '接受教育不能为空',

        ];
        $validate=Validator::make($datas,$rules,$message);
        if($validate->fails())
        {
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else{
        $oExperts=Expert::find($id);
        if($request->hasFile('thumb'))
        {
            $thumb=$request->file('thumb');
            $path="/upload/".$thumb->store('images','upload');
            $oExperts['thumb']=$path;
        }

        $oExperts->name=$request['name'];
        $oExperts->keshi=$request['keshi'];
        $oExperts->zhicheng=$request['zhicheng'];
        $oExperts->zhiwu=$request['zhiwu'];
        $oExperts->yiyuan=$request['yiyuan'];
        $oExperts->jianli=$request['jianli'];
        $oExperts->jieshoujiaoyu=$request['jieshoujiaoyu'];
        $oExperts->save();
        return json_encode(['success'=>true,'msg'=>"success"]);}





    }
    public function destroy($id)
    {
        $oExperts=Expert::find($id);

        $oExperts->delete();
        return redirect('/adm/expertlist');







    }
    public function ExpertList(){
        $oExperts=Expert::all();
        return view('admin.expert')->with("oExperts",$oExperts);
    }

    public function expert(){

        return view('front.expert');
    }
    public function expertinfo($id){
        return view('front.expert-info');
    }
}
